package com.coders.activities;

import org.cocos2d.sound.SoundEngine;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.ToggleButton;

import com.coders.R;
import com.coders.configuration.SettingsConfig;
import com.coders.info.MusicState;
import com.coders.info.SoundState;
import com.coders.util.Logger;

/**
 * Main Menu of the application.
 * 
 * @author Subin
 */
public class MainMenu extends BaseActivity
{
    private static MediaPlayer buttonCLickPlayer;
    private Animation          animation;
    private ImageView          newGameImgView;
    private ImageView          resumeGameImgView, HighscoreImgView;
    private ImageView          settingsImgView, fb, twitter, exit;
    private ToggleButton       musiconoff;
    private ToggleButton soundonoff;

    String                     SOUND;
    String Music;
    private static boolean     isgamestart = false;

    @Override
    public void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.main_menu );
        SOUND = SettingsConfig.getSelectedSoundState();
        Music=SettingsConfig.getSelectedMusicState();

        isgamestart = false;

        initViewComponents();
        musiconoff.setTextOff( "" );
        musiconoff.setTextOn( "" );
        soundonoff.setTextOff( "" );
        soundonoff.setTextOn( "" );
        setListenersForViews();
        if ( SettingsConfig.getSelectedMusicState() == "OFF" )
        {
            Logger.verbose( "getSelectedMusicState()" +SettingsConfig.getSelectedMusicState());
            musiconoff.setBackgroundResource( R.drawable.musictrackoff );

        }
        else
        {
            musiconoff.setBackgroundResource( R.drawable.musictrackon);
            Logger.verbose( "getSelectedMusicState()" +SettingsConfig.getSelectedMusicState());


        }
        if ( SettingsConfig.getSelectedSoundState() == "OFF" )
        {
           soundonoff.setBackgroundResource( R.drawable.gamemusicoff );
           Logger.verbose( "getSelectedSoundState()" +SettingsConfig.getSelectedSoundState());


        }
        else
        {
            soundonoff.setBackgroundResource( R.drawable.gamemusicon );
            Logger.verbose( "getSelectedSoundState()" +SettingsConfig.getSelectedSoundState());


        }

    }

    private void initViewComponents()
    {
        newGameImgView = ( ImageView ) findViewById( R.id.newGameImgView );

        musiconoff = ( ToggleButton ) findViewById( R.id.ToggleButton2 );
        soundonoff= ( ToggleButton ) findViewById( R.id.ToggleButton1 );
        HighscoreImgView = ( ImageView ) findViewById( R.id.HighscoreImgView );
        exit = ( ImageView ) findViewById( R.id.HelpImgView );

        fb = ( ImageView ) findViewById( R.id.fbImgView );

        twitter = ( ImageView ) findViewById( R.id.twitterImgView );
    }

    private void setListenersForViews()
    {

        newGameImgView.setOnClickListener( new OnClickListener()
        {
            public void onClick( View v )
            {
                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences( MainMenu.this
                        .getApplicationContext() );
                Editor editor = prefs.edit();
                editor.putBoolean( "startgame", true );
                editor.commit();

                isgamestart = true;

                SoundEngine.sharedEngine().realesAllSounds();
                SoundEngine.sharedEngine().realesAllEffects();

                SoundEngine.purgeSharedEngine();
                Intent playIntent = new Intent( MainMenu.this, Play.class );
                playIntent.putExtra( "RESUME_YN", "N" );

                MainMenu.this.startActivity( playIntent );

                finish();

            }

        } );
        HighscoreImgView.setOnClickListener( new OnClickListener()
        {

            @Override
            public void onClick( View v )
            {
                Intent playIntent = new Intent( MainMenu.this, ScoreCard.class );
                MainMenu.this.startActivity( playIntent );
                finish();
            }
        } );

       soundonoff.setOnClickListener( new OnClickListener()
        {
            public void onClick( View v )
            {

                //if (soundonoff.isChecked() )
                if (  SOUND!="OFF" )


                {

                    SOUND = SoundState.OFF.toString();
                    soundonoff.setBackgroundResource( R.drawable.gamemusicoff);
                    SettingsConfig.setSelectedSoundState( SOUND );
                    Logger.verbose( "SOUND OFF" );

                }
                else
                {
                    SOUND = SoundState.ON.toString();

                    soundonoff.setBackgroundResource( R.drawable.gamemusicon );
                    SettingsConfig.setSelectedSoundState( SOUND );
                    Logger.verbose( "SOUND ON" );


                }

            }
        } );
       musiconoff.setOnClickListener( new OnClickListener()
    {
        
        @Override
        public void onClick( View v )
        {
            //if (musiconoff.isChecked() )
            if(Music!="OFF")
            {

                Music = MusicState.OFF.toString();
                musiconoff.setBackgroundResource( R.drawable.musictrackoff);
                SettingsConfig.setSelectedMusicState( Music );
                Logger.verbose( "MUSIC OFF" );

            }
            else
            {
                Music = MusicState.ON.toString();

                musiconoff.setBackgroundResource( R.drawable.musictrackon );
                SettingsConfig.setSelectedMusicState( Music );
                Logger.verbose( "MUSIC ON" );


            }
            
        }
    });
        twitter.setOnClickListener( new OnClickListener()
        {

            @Override
            public void onClick( View v )
            {
                Uri uri = Uri.parse( "https://twitter.com/CodersGlobal" );
                Intent intent = new Intent( Intent.ACTION_VIEW, uri );
                startActivity( intent );

            }
        } );
        exit.setOnClickListener( new OnClickListener()
        {

            @Override
            public void onClick( View v )
            {
                Intent playIntent = new Intent( MainMenu.this, help.class );
                MainMenu.this.startActivity( playIntent );

            }
        } );
        fb.setOnClickListener( new OnClickListener()
        {

            @Override
            public void onClick( View v )
            {

                Uri uri = Uri
                        .parse( "http://www.facebook.com/pages/Coders-Global-Software-Solutions-Pvt-Ltd/297389563651651" );
                Intent intent = new Intent( Intent.ACTION_VIEW, uri );
                startActivity( intent );

            }
        } );

    }

    /**
     * Apply animation transitions.
     */

    @Override
    public boolean onKeyDown( int keyCode, KeyEvent event )
    {

        showExitGameDialog();
        return super.onKeyDown( keyCode, event );

    }

    public void showExitGameDialog()
    {
        AlertDialog.Builder localBuilder = new AlertDialog.Builder( this );
        localBuilder.setMessage( "Are you sure to quit game?" );
        localBuilder.setCancelable( false );
        localBuilder.setPositiveButton( "Quit", new DialogInterface.OnClickListener()
        {
            public void onClick( DialogInterface paramDialogInterface, int paramInt )
            {

                Intent intent = new Intent();
                setResult( RESULT_CANCELED, intent );
                finish();

            }
        } );
        localBuilder.setNegativeButton( "Continue", new DialogInterface.OnClickListener()
        {
            public void onClick( DialogInterface paramDialogInterface, int paramInt )
            {
                paramDialogInterface.cancel();

            }
        } );
        localBuilder.show();
    }

}